package cn.tedu.ivos.repair.service.impl;

import cn.tedu.ivos.repair.mapper.RepairMapper;
import cn.tedu.ivos.repair.pojo.dto.RepairQuery;
import cn.tedu.ivos.repair.pojo.dto.RepairSaveParam;
import cn.tedu.ivos.repair.pojo.entity.Repair;
import cn.tedu.ivos.repair.pojo.vo.RepairVO;
import cn.tedu.ivos.repair.service.RepairService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class RepairServiceImpl implements RepairService {
    @Autowired
    VehicleMapper vehicleMapper;
    @Autowired
    RepairMapper repairMapper;


    @Override
    public void save(RepairSaveParam repairSaveParam) {
        Repair repair = new Repair();
        BeanUtils.copyProperties(repairSaveParam,repair);

        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setId(repairSaveParam.getVehicleId());
        VehicleVO vehicleVO = vehicleMapper.selectVehicle(vehicleQuery).get(0);
        repair.setVehicleLicense(vehicleVO.getLicense());
        Vehicle vehicle = new Vehicle();
        vehicle.setId(repairSaveParam.getVehicleId());
        if("0".equals(repairSaveParam.getStatus())){
            vehicle.setStatus("1");
        }else if ("1".equals(repairSaveParam.getStatus())){
            vehicle.setStatus("3");
        }
        vehicleMapper.update(vehicle);
        if(repairSaveParam.getId()==null){
            repair.setCreateTime(new Date());
            repairMapper.insert(repair);
        }else{
            repair.setUpdateTime(new Date());
            repairMapper.update(repair);
        }

    }

    @Override
    public List<RepairVO> select(RepairQuery repairQuery) {
        List<RepairVO> list = repairMapper.select(repairQuery);
        return list;
    }

    @Override
    public void delete(Long id) {
        repairMapper.delete(id);
    }
}
